package com.minio.controller;

import com.minio.util.MinioUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class MinioClientController {
    private static Logger logger = LoggerFactory.getLogger(MinioClientController.class);
    @Autowired
    private MinioUtils minioUtilS;

    /**
     * 支持多文件上传
     *
     * @param multipartFile
     * @return
     */
    @PostMapping("/upload")
    public Object upLoad(MultipartFile[] multipartFile) {
        for (MultipartFile file : multipartFile) {
            logger.info("上传文件名称是: ", file.getOriginalFilename());
        }

        if (multipartFile.length == 0) {
            return "上传文件是空的，请重新上传";
        }

        List<String> uploadList = minioUtilS.upload(multipartFile);
        if (uploadList.size() == 0) {
            return "上传文件失败";
        }

        StringBuffer returnStr = new StringBuffer();
        for (String fileName : uploadList) {
            returnStr.append(fileName + ";");
        }
        return returnStr;
    }


    /**
     * 下载单个文件
     *
     * @param fileName
     * @return
     */
    @PostMapping("/download")
    public Object download(String fileName) {

        if (fileName.isEmpty() || fileName == null) {
            return "下载文件的名称是空的";
        }

        ResponseEntity<byte[]> responseEntity = minioUtilS.download(fileName);
        if (responseEntity == null) {
            return "下载的文件为空,请确定文件是否存在";
        }
        return responseEntity;
    }

    /**
     * 删除单个文件
     *
     * @param fileName
     * @return
     */
    @PostMapping("/removeObject")
    public Object removeObject(String fileName) {

        if (fileName.isEmpty() || fileName == null) {
            return "删除文件的名称是空的";
        }

        boolean isOk = minioUtilS.removeObject(fileName);
        return isOk;
    }

    /**
     * 删除多个文件
     *
     * @param filenames
     * @return
     */
    @PostMapping("/removeObjects")
    public Object removeObjects(@RequestParam("filenames") List<String> filenames) {

        if (filenames.size() == 0) {
            return "删除文件列表是空的";
        }
        boolean isOk = minioUtilS.removeObjects(filenames);
        return isOk;
    }

    /**
     * 删除桶示例
     *
     * @param bucketName
     * @return
     */
    @PostMapping("/removeBucket")
    public Object removebucket(String bucketName) {

        if (bucketName.isEmpty() || bucketName == null) {
            return "删除的桶名称是空的";
        }

        boolean isOk = minioUtilS.removeBucket(bucketName);
        return isOk;
    }



}


